package com.demo.villageresidence.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.villageresidence.dto.FeedDTO;
import com.demo.villageresidence.dto.Result;
import com.demo.villageresidence.dto.VehicleDTO;
import com.demo.villageresidence.pojo.Feedback;
import com.demo.villageresidence.pojo.FeedbackReply;
import com.demo.villageresidence.pojo.User;
import com.demo.villageresidence.pojo.VehicleApproval;
import com.demo.villageresidence.service.FeedbackReplyService;
import com.demo.villageresidence.service.FeedbackService;
import com.demo.villageresidence.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/feedbackReply")
public class FeedbackReplyController {

    @Autowired
    private FeedbackReplyService feedbackReplyService;

    @Autowired
    private FeedbackService feedbackService;

    @Autowired
    private UserService userService;

    @PostMapping("addFeedbackReply")
    public Result addFeedbackReply(@RequestBody FeedbackReply feedbackReply) {

        feedbackReply.setIsDelete(0);
        feedbackReply.setReplyTime(LocalDateTime.now());
        feedbackReply.setReplyStatus(1);

        if (feedbackReplyService.save(feedbackReply)) {
            feedbackService.update().set("feed_status", 1).eq("feed_id", feedbackReply.getFeedId()).update();
            return Result.ok("处理成功");
        } else {
            return Result.fail("处理失败");
        }
    }

    @GetMapping("getFeedPage")
    public Result getFeedPage(@RequestParam(value = "current", defaultValue = "1") Integer current,
                              @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        Page<FeedbackReply> page = feedbackReplyService.query().eq("reply_status", 1)
                .page(new Page<>(current, pageSize));


        // 获取当前页数据
        List<FeedbackReply> feedbackReplyList = page.getRecords();

        List<FeedDTO> feedDTOList = new ArrayList<>();

        feedbackReplyList.forEach(feedbackReply -> {
            FeedDTO feedDTO = setFeedDTO(feedbackReply);
            feedDTOList.add(feedDTO);
        });

        long total = page.getTotal();
        return Result.ok(feedDTOList,total);
    }

    private FeedDTO setFeedDTO(FeedbackReply feedbackReply) {
        Feedback feedback = feedbackService.getById(feedbackReply.getFeedId());
        FeedDTO feedDTO = new FeedDTO();
        feedDTO.setFeedId(feedbackReply.getFeedId());
        feedDTO.setReplyId(feedbackReply.getReplyId());
        feedDTO.setReplyContent(feedbackReply.getReplyContent());
        feedDTO.setReplyTime(feedbackReply.getReplyTime());
        feedDTO.setReplyStatus(feedbackReply.getReplyStatus());
        feedDTO.setAdminId(feedbackReply.getAdminId());
        feedDTO.setFeedDescription(feedback.getFeedDescription());
        feedDTO.setFeedQuestion(feedback.getFeedQuestion());
        feedDTO.setFeedTime(feedback.getFeedTime());
        feedDTO.setFeedStatus(feedback.getFeedStatus());
        feedDTO.setUserId(feedback.getUserId());
        feedDTO.setUserName(userService.getById(feedback.getUserId()).getUserName());
        feedDTO.setUserPhone(userService.getById(feedback.getUserId()).getUserPhone());
        return feedDTO;
    }


    @GetMapping("getFeedByReplyId/{id}")
    public Result getFeedByReplyId(@PathVariable("id") Integer replyId) {

        FeedbackReply feedbackReply = feedbackReplyService.getById(replyId);
        FeedDTO feedDTO = setFeedDTO(feedbackReply);
        return Result.ok(feedDTO);
    }

    @GetMapping("getFeedByUserId/{id}")
    public Result getFeedByUserId(@PathVariable("id") Integer userId) {

        List<Feedback> feedbackList = feedbackService.query().eq("user_id", userId).list();

        List<FeedDTO> feedDTOList = new ArrayList<>();

        feedbackList.forEach(feedback -> {
            FeedbackReply feedbackReply = feedbackReplyService.query().eq("feed_id", feedback.getFeedId()).one();

            FeedDTO feedDTO = setFeedDTO(feedbackReply);
            feedDTOList.add(feedDTO);

        });

        if (feedDTOList.size() == 0) {
            return Result.fail("暂无数据");
        }

        return Result.ok(feedDTOList);
    }




}
